Mastering+Object-oriented+Python.compressed by Unknown
Author:Unknown
Language: eng
Format: mobi
Tags: www.it-ebooks.info
Published: 2015-09-10T19:50:08.116379+00:00
======================================================================
FAIL: test_should_returnStr (__main__.TestAceCard)
----------------------------------------------------------------------
Traceback (most recent call last):
File "p3_c15.py", line 80, in test_should_returnStr
self.assertEqual( "A♠", str(self.ace_spades) )
AssertionError: 'A♠' != '1♠'
- A♠
+ 1♠
[ 485 ]
Designing for Testability
======================================================================
FAIL: test_should_returnStr (__main__.TestFaceCard)
----------------------------------------------------------------------
Traceback (most recent call last):
File "p3_c15.py", line 91, in test_should_returnStr
self.assertEqual( "Q♥", str(self.queen_hearts) )
AssertionError: 'Q♥' != '12♥'
- Q♥
+ 12♥
----------------------------------------------------------------------
Ran 6 tests in 0.001s
FAILED (failures=2)
The TestLoader class created two tests from each TestCase class. This gives us a total of six tests. The test names are the method names that begin with test.
Clearly, we have a problem. Our tests provide an expected result that our class definitions don't meet. We've got more development work to do for the Card classes in order to pass this simple suite of unit tests. The fix should be clear and we'll leave it as an exercise for the reader.
Including edge and corner cases
When we move to testing the Deck class as a whole, we'll need to have some things confirmed: that it produces all of the required Cards class, and that it actually shuffles properly. We don't really need to test that it deals properly because we're depending on the list and list.pop() method; as these are first-class parts of Python, they don't require additional testing.
We'd like to test the Deck class construction and shuffling, independently of any specific Card class hierarchy. As noted previously, we can use a factory function to make the two Deck and Card definitions independent. Introducing a factory function introduces yet more testing. Not a bad thing, considering the bugs previously revealed in the Card class hierarchy.
Here's a test of the factory function:
class TestCardFactory( unittest.TestCase ):
def test_rank1_should_createAceCard( self ):
c = card( 1, '♣' )
self.assertIsInstance( c, AceCard )
def test_rank2_should_createCard( self ):
[ 486 ]
Chapter 15
c = card( 2, '♦' )
self.assertIsInstance( c, Card )
def test_rank10_should_createCard( self ):
c = card( 10, '♥' )
self.assertIsInstance( c, Card )
def test_rank10_should_createFaceCard( self ):
c = card( 11, '♠' )
self.assertIsInstance( c, Card )
def test_rank13_should_createFaceCard( self ):
c = card( 13, '♣' )
self.assertIsInstance( c, Card )
def test_otherRank_should_exception( self ):
with self.assertRaises( LogicError ):
c = card(14, '♦')
with self.assertRaises( LogicError ):
c = card(0, '♦')
We didn't test all 13 ranks, as 2 through 10 should all be identical. Instead, we followed this advice from Boris Beizer:
"Bugs lurk in corners and congregate at boundaries."
The test cases involve the edge values for each card range. Consequently, we have test cases for the values 1, 2, 10, 11, and 13, as well as illegal values of 0 and 14. We bracketed each range with the least value, the maximum value, one below the least value, and one above the maximum value.
When this is run, there will be problems reported by this test case too. One of the biggest problems will be an undefined exception, LogicError. This is simply a subclass of Exception that defines that the exception still isn't enough to get the test case to pass. The rest of the fix is left as an exercise for the reader.
Mocking dependencies for testing
In order to test Deck, we have two choices to handle the dependencies:
• Mocking: We can create a mock (or stand-in) class for the Card class and a mock card() factory function that produces the mock class.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Personalized inhaled bacteriophage therapy for treatment of multidrug-resistant Pseudomonas aeruginosa in cystic fibrosis by unknow(200926)
CONSORT 2025 statement: updated guideline for reporting randomized trials by unknow(102424)
Critical evaluation of the ProfiLER-02 study design and outcomes by Vivek Subbiah & Razelle Kurzrock(101959)
Cardiac gene therapy makes a comeback by Oliver J. Müller & Susanne Hille & Anca Kliesow Remes(101757)
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(74556)
Unveiling the design rules for tunable emission in graphene quantum dots: A high-throughput TDDFT and machine learning perspective by Şener Özönder & Mustafa Coşkun Özdemir & Caner Ünlü(50973)
A yeast-based oral therapeutic delivers immune checkpoint inhibitors to reduce intestinal tumor burden by unknow(40345)
Covalent hitchhikers guide proteins to the nucleus by Alexander F. Russell & Madeline F. Currie & Champak Chatterjee(40267)
Meet the Authors: Christopher R. Mansfield and Emily R. Derbyshire by Christopher R. Mansfield & Emily R. Derbyshire(40169)
Alkaline-earth metals promote propane dehydrogenation with carbon dioxide through geometric effects: Altering the reaction pathway by unknow(32795)
Induced iron vacancies boosting FeOOH loaded on sustainable Fenton-like collagen fiber membrane for efficient removal of emerging contaminants by unknow(32589)
Efficient electric-field-assisted photochemical conversion of methane to n-propanol exclusively over penetrated TiO2Ti hollow fibers by Guanghui Feng(32509)
Bi2SiO5 nanosheets as piezo-photocatalyst for efficient degradation of 2,4-Dichlorophenol by Hangyu Shi & Yifu Li & Lishan Zhang & Guoguan Liu & Qian Zhang & Xuan Ru & Shan Zhong(32450)
A novel NDIPTA organic heterojunction photocatalyst with built-in electric field for efficient hydrogen production by Jiahui Yang & Baojun Ma & Yongfa Zhu(32420)
Enhanced conversion of methane to liquid-phase oxygenates via hollow ferrite nanotube@horseradish peroxidase based photoenzymatic catalysis by Jun Duan & Shiying Fan & Xinyong Li & Shaomin Liu(32388)
Ordered macroporous superstructure of defective carbon adorned with tiny cobalt sulfide for selective electrocatalytic hydrogenation of cinnamaldehyde by Xiao-Shi Yuan & Sheng-Hua Zhou & San-Mei Wang & Wenbo Wei & Xiaofang Li & Xin-Tao Wu & Qi-Long Zhu(32307)
What's Done in Darkness by Kayla Perrin(27218)
Topological analysis of non-conjugated ethylene oxide cored dendrimers decorated with tetraphenylethylene: Insights from degree-based descriptors using the polynomial approach by A Theertha Nair & D Antony Xavier & Annmaria Baby & S Akhila(26600)
Investigation of mechanical and self-healing properties of hydroxyl-terminated polybutadiene functionalized with 2-ureido-4-pyrimidinone by Mohsen Kazazi & Mehran Hayaty & Ali Mousaviazar(26521)